//
//  SinaHomeCell.swift
//  MySinaWeiBoSwiftProject
//
//  Created by apple on 2016/11/16.
//  Copyright © 2016年 WangChen. All rights reserved.
//

import UIKit
import SDWebImage


let margin: CGFloat = 8
let imageMargin: CGFloat = 2
let maxWidth = screenWidth - 2 * margin
let itemWidth = (maxWidth - 2 * imageMargin)/3

class SinaHomeCell: UITableViewCell {

    
    @IBOutlet weak var iv_icon: UIImageView!
    
    @IBOutlet weak var lb_name: UILabel!
    
    @IBOutlet weak var lb_time: UILabel!
    
    @IBOutlet weak var lb_source: UILabel!
    
    @IBOutlet weak var lb_content: YYLabel!
    
    @IBOutlet weak var iv_rank: UIImageView!
    
    @IBOutlet weak var iv_vip: UIImageView!
    
    @IBOutlet weak var pictureView: SinaHomePictureView!
    
    //转发的内容lable
    @IBOutlet weak var lb_retweeteContent: YYLabel!
    
    //图片视图宽高约束
    @IBOutlet weak var constraintH: NSLayoutConstraint!
    
    @IBOutlet weak var constraintW: NSLayoutConstraint!
    
    //图片视图布局
    @IBOutlet weak var pictureLayout: UICollectionViewFlowLayout!
    
    
    @IBOutlet weak var toolBarView: UIView!
    
    
    @IBOutlet weak var pictureViewConstraintTop: NSLayoutConstraint!
    
    var rowHeight: CGFloat{
        return 37 + toolBarView!.frame.origin.y
    }
    
    
    let margin: CGFloat = 8
    
    
    override func awakeFromNib() {
        super.awakeFromNib()
        // Initialization code
        iv_icon.layer.masksToBounds = true
        lb_content.preferredMaxLayoutWidth = screenWidth - margin * 2
        if lb_retweeteContent != nil {
            lb_retweeteContent.preferredMaxLayoutWidth = screenWidth - margin * 2
        }
        
        pictureLayout.itemSize = CGSize(width: itemWidth, height: itemWidth)
        pictureLayout.minimumLineSpacing = imageMargin
        pictureLayout.minimumInteritemSpacing = imageMargin
        
        //选中的样式 设为空样式
        selectionStyle = .none
        }
    
    
    var homeCellViewModel: SinaHomeCellViewModel?{
        
        didSet{
            iv_icon.sd_setImage(with: homeCellViewModel?.iconUrl)
            iv_rank.image = homeCellViewModel?.rankImage
            iv_vip.image = homeCellViewModel?.verifitedImage
            lb_name.text = homeCellViewModel?.statusModel?.user?.name
            lb_time.text = homeCellViewModel?.creatDateStr
            lb_source.text = homeCellViewModel?.sourceStr
            
            lb_content.attributedText = homeCellViewModel?.originAttributeString
            lb_retweeteContent?.attributedText = homeCellViewModel?.retweeterAttributeString
            
            //得到图片的数量
            let count = homeCellViewModel?.imageURLs?.count
            let pSize = layoutThePictureViewWithCount(count: count!).0
            constraintH.constant = pSize.height
            constraintW.constant = pSize.width
            
            pictureViewConstraintTop.constant = (count == 0 ? 0 : 8)
            pictureLayout.itemSize = layoutThePictureViewWithCount(count: count!).1
            
            pictureView.layoutIfNeeded()
            pictureView.pictureInfoModelList = homeCellViewModel?.imageURLs
            
            //实现点击高亮的闭包
            //void(^YYTextAction)(UIView *containerView, NSAttributedString *text, NSRange range, CGRect rect)
            lb_content.highlightTapAction = {
                (containerView,text,range,rect) -> () in
                
                let sub = (text.string as NSString).substring(with: range)
                if sub.hasPrefix("http"){
                    let web = SinaHomeWebViewController()
                    web.urlStr = sub
                    let naviVC = self.getNaviVCResponder()
                    naviVC?.pushViewController(web, animated: true)
                }
                
            }
            
            
        }
        
    }
    
    
    
    //根据图片数量来布局图片视图(计算配图视图的大小)
    private func layoutThePictureViewWithCount(count: Int)->(CGSize,CGSize){
        if count == 0{
            return (CGSize(width: 1,height: 1), CGSize(width: 1,height: 1))
        }
        if count == 1{
            let imageUrl = homeCellViewModel?.statusModel?.pic_urls?.first?.wap360URLStr
            
            if let image = SDWebImageManager.shared().imageCache.imageFromDiskCache(forKey: imageUrl){
                
                return(image.size,image.size)
            }
            
            return (CGSize(width: itemWidth, height: itemWidth),CGSize(width: itemWidth, height: itemWidth))
        }
        if count == 4{
            let width = 2 * itemWidth + margin
            return (CGSize(width: width, height: width), CGSize(width: itemWidth, height: itemWidth))
        }
        
        let row = (count - 1)/3 + 1
        let w = maxWidth
        let h = itemWidth * CGFloat(row) + imageMargin * CGFloat(row - 1)
        return (CGSize(width: w, height: h),CGSize(width: itemWidth, height: itemWidth))
    }
    
    
    
    
    

    override func setSelected(_ selected: Bool, animated: Bool) {
        super.setSelected(selected, animated: animated)

        // Configure the view for the selected state
    }
    
}
